package com.tanhua.dubbo.service.api;


import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.api.DashboardApi;

import com.tanhua.dubbo.pojo.LoginUser;
import com.tanhua.dubbo.pojo.BaseDistribution;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;


import java.util.List;

@Service
public class DashboardApiImpl implements DashboardApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 分月查询
     *
     * @param sdp
     * @param edp
     * @param month
     * @return
     */
    @Override
    public BaseDistribution getCountMonth(Long sdp, Long edp, int month) {
        //查询月份时间区间的用户信息
        Query query = Query.query(Criteria.where("loginTime").gte(sdp).lte(edp));
        List<Long> loginUserList = mongoTemplate.findDistinct(query, "userId", "login_log", Long.class);

        BaseDistribution baseDistribution = new BaseDistribution();
        baseDistribution.setTitle(month + "月");
        baseDistribution.setAmount(loginUserList.size());

        return baseDistribution;
    }

    /**
     * 活跃用户
     *
     * @param sdp
     * @param edp
     * @return
     */
    @Override
    public Integer getCountActive(Long sdp, Long edp) {
        Query query = Query.query(Criteria.where("loginTime").gte(sdp).lte(edp));
        List<Long> loginUserList = mongoTemplate.findDistinct(query, "userId", "login_log", Long.class);
        //List<LoginUser> loginUserList = mongoTemplate.find(query, LoginUser.class);
        return loginUserList.size();
    }

    /**
     * 查询登录次数
     * @param sdp
     * @param edp
     * @return
     */
    public Integer getCountLogin(Long sdp, Long edp) {
        Query query = Query.query(Criteria.where("loginTime").gte(sdp).lte(edp));
        List<LoginUser> loginUserList = mongoTemplate.find(query,LoginUser.class);
        return loginUserList.size();
    }
}
